package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.feign.wemedia.IWmUserClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealNameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;

@Slf4j
@Service
@Transactional
public class ApUserRealNameServiceImpl implements ApUserRealNameService {
    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private ApUserRealNameMapper apUserRealNameMapper;

    @Autowired
    private IWmUserClient wmUserClient;

    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        Integer currentPage = dto.getPage();
        Integer size = dto.getSize();


        Short status = dto.getStatus();

        Page<ApUserRealname> page = new Page<>(currentPage, size);

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(status != null, ApUserRealname::getStatus, status);

        page = apUserRealNameMapper.selectPage(page, queryWrapper);

        Long total = page.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage, size, total.intValue());

        pageResponseResult.setData(page.getRecords());


        return pageResponseResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult updateStatus(AuthDto dto, Integer type) {
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer id = dto.getId();
        //数据库查一下,看有没有
        ApUserRealname apUserRealname = apUserRealNameMapper.selectById(id);
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //更新
        apUserRealname.setStatus((short) (type == 1 ? 9 : 2));
        apUserRealname.setUpdatedTime(new Date());
        apUserRealname.setReason(dto.getMsg());
        apUserRealNameMapper.updateById(apUserRealname);

        //判断是驳回还是通过
        if (1 == type) {//通过则需要创建自媒体账号,更新ap用户信息flag=1
            Integer userId = apUserRealname.getUserId();
            String name = apUserRealname.getName();
            HashMap<String, String> map = new HashMap<>();
            map.put("name",name);
            //feign远程调用自媒体服务
            ResponseResult responseResult = wmUserClient.addUser(map);//查询自媒体是否存在  创建自媒体账号
            if (responseResult == null || responseResult.getCode() != 200) {
                throw new RuntimeException("自媒体用户创建失败");
            }

            //更新ApUser的flag=1:自媒体人
            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            int updateResult = apUserMapper.updateById(apUser);

            if (updateResult < 1) {
                throw new RuntimeException("更新apUser的flag字段失败");
            }
        }

        return ResponseResult.okResult(apUserRealname);
    }
}
